From c75d394eeae2f9423ff08f2c5eb88ecd16bd5548 Mon Sep 17 00:00:00 2001 From: robertl Date: Sat, 7 Oct 2006 17:38:56 +0000 Subject: [PATCH] After kicking around many too-complicated solutions, apply the buffer reallocation fix for gbfile. Tested on Windows and Linux with forced buffer reallocs. git-svn-id: http://gpsbabel.googlecode.com/svn/trunk@2381 f51c46e8-681c-474f-0cfe-069cfd0219fb --- gpsbabel/gbfile.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/gpsbabel/gbfile.c b/gpsbabel/gbfile.c index f4773a3c4..ef60719a6 100644 --- a/gpsbabel/gbfile.c +++ b/gpsbabel/gbfile.c @@ -261,17 +261,26 @@ gbfprintf(gbfile *file, const char *format, ...) len = vsnprintf(file->buff, file->buffsz, format, args); va_end(args); - if (len < 0) - fatal(MYNAME ": Unexpected vsnprintf error %d (%s/%s)!\n", - len, file->module, file->name); - else if (len == 0) - return 0; - else if (len < file->buffsz) + /* Unambiguous Success */ + if ((len > -1) && (len < file->buffsz)) break; - while (file->buffsz <= len) + /* First case: C99 behaviour. Len is correctly sized. + * add space for null terminator. Next time through the + * loop we're guaranteed success. + * + * Second case: SUS (and Windows) behaviour. We know it + * doesn't fit, but we don't know how big it has to be. +` * double it and try again. We'll loop until we succeed. + * + * Since we keep the I/O buffer in the file handle, we + * quickly reach a steady state on the size of these buffers. + */ + if (len > -1) + file->buffsz = len + 1; + else file->buffsz *= 2; - + file->buff = xrealloc(file->buff, file->buffsz); } return gbfwrite(file->buff, 1, len, file); -- 2.30.2